package com.party.controller.sys.sysManage;

import com.party.common.Page;
import com.party.model.SysRole;
import com.party.model.SysRoleAccess;
import com.party.service.sys.MenuService;
import com.party.service.sys.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统角色管理
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-4-10
 * Time: 下午12:50
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/sys/roleManage")
public class RoleManageController {
    @Resource
    RoleService roleService;
    @Resource
    MenuService menuService;
    private int pageSize = 9;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sys/sysmange/sys_group");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String name = req.getParameter("name");
        String status = req.getParameter("status");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        SysRole sysRole = new SysRole();
        sysRole.setName(name);
        if(status != null && !"".equals(status))
        {
            sysRole.setStatus(Integer.parseInt(status));
        }
        Page p = this.roleService.getList(sysRole, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add(
            @RequestParam(value = "msg", required = false) String msg,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();

        String roleid = request.getParameter("roleid");
        if (roleid == null || "".equals(roleid)) {

        } else {
            List<SysRoleAccess> sysRoleAccesses = roleService.getRoleAccesssByRoleId(Integer.parseInt(roleid));
            modelAndView.addObject("sysRoleAccesses", sysRoleAccesses);
            SysRole sysRole = roleService.getSysRoleById(Integer.parseInt(roleid));
            modelAndView.addObject("sysRole", sysRole);
        }
        String info = menuService.getNodesStr();
        modelAndView.addObject("nodeList", info);

        modelAndView.addObject("msg", msg);
        modelAndView.setViewName("sys/sysmange/sys_group_add");
        return modelAndView;
    }


    @RequestMapping(value = "/addData", method = RequestMethod.POST)
    public ModelAndView addData(
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        String roleid = request.getParameter("roleid");
        String status = request.getParameter("status");
        String rolename = request.getParameter("rolename");
        String nodes[] = request.getParameterValues("node");

        int result = 0;
        if (roleid == null || "".equals(roleid)) {
            result = roleService.addAccess(rolename,Integer.parseInt(status),nodes);
        } else {
            SysRole sysRole = new SysRole();
            sysRole.setName(rolename);
            sysRole.setStatus(Integer.parseInt(status));
            sysRole.setRoleid(Integer.parseInt(roleid));
            result = roleService.updateAccess(sysRole, nodes);
        }

        String msg = "";
        if (result == 1) {
            msg = "恭喜，添加成功！";
        } else if (result == -2) {
            msg = "角色名称已经存在！";
        } else {
            msg = "添加失败！";
        }

        modelAndView.addObject("msg", msg);
        modelAndView.setViewName("redirect:/sys/roleManage/index.do");
        return modelAndView;
    }

    /**
     * 角色删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> del(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = roleService.del(ids);
        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }

}
